package kr.ac.mju.mdc.mjuapp.photosns;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import kr.ac.mju.mdc.mjuapp.MainActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * @author davidkim
 *
 */
public class PhotoSNSThread extends Thread {
	private Handler handler;
	private String[] imageUrls = { "http://www.mju.ac.kr/mbs/mjukr/download/1.jpg", "http://www.mju.ac.kr/mbs/mjukr/download/2.jpg",
			"http://www.mju.ac.kr/mbs/mjukr/download/3.jpg", "http://www.mju.ac.kr/mbs/mjukr/download/4.jpg",
			"http://www.mju.ac.kr/mbs/mjukr/download/5.jpg" };

	public PhotoSNSThread(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler = handler;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		// debug
		long start = System.currentTimeMillis();
		for (int index = 0; index < imageUrls.length; index++) {
			try {
				URL imageUrl = new URL(imageUrls[index]);
				HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
				conn.setDefaultUseCaches(true);
				conn.connect();
				int size = conn.getContentLength();
				if (size > 0) {
					BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), size);
					Bitmap bitmap = BitmapFactory.decodeStream(bis);
					Message msg = new Message();
					msg.what = MainActivity.IMAGE_BOARD_COMPLETE;
					msg.obj = bitmap;
					handler.sendMessage(msg);
					bis.close();
				}
				conn.disconnect();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.d("MDC", "ImageBoardThread/IOException : " + e.toString());
			}
		}
		// debug
		long end = System.currentTimeMillis();
		Log.d("MDC", "ImageBoardThread : " + (double) (end - start) / 1000.);
	}
}
/* end of file */
